<?php
/**
 * Created by PhpStorm.
 * User: mhx
 */

class yachangController extends baseController
{
    static protected $mVisitPath = '';//图片访问路径
    static protected $mFilePath = '';
    static protected $mUrlHomepage = '';
    static protected $easyAI = '';

    public function __construct()
    {
        parent::__construct();
        self::$mFilePath = ROOT_PATH . "exhibits/";
        if (!is_dir(self::$mFilePath)) mkdir(self::$mFilePath);
        //要分析的网站主页
        self::$mUrlHomepage = '';
        self::$easyAI = 0;
    }

    public function getContent($isreadihstory = NULL, $endtime = 1)
    {
        $this->analysePageOne($endtime);
        if ($isreadihstory) {
            //历史展览多页
            $returnback = 'ok';
            $url_num = 0;
            while ('timeout' != $returnback) {
                $returnback = $this->analyseHistory($url_num, $endtime);
                ++$url_num;
            }
        }
        return null;
    }

    protected function analysePageOne($endtime = 1)
    {
        //最终所得后缀url的前缀
        //echo 'yes';die;
        $url_prefix = 'http://exhibit.artron.net';
        //要分析的页面
        //http://exhibit.artron.net/exhibition/1-0-0     全部展览
        //http://exhibit.artron.net/exhibition/2-0-0     最新发布
        //http://exhibit.artron.net/exhibition/3-0-0     今日开幕
        //http://exhibit.artron.net/exhibition/4-0-0     即将开展
        //http://exhibit.artron.net/exhibition/5-0-0     正在展览
        //http://exhibit.artron.net/exhibition/6-0-0     已结束展览
        //最新发布有4000多页，只取前20页

        $url = 'http://exhibit.artron.net/exhibition/4-0-0';
        $page = $this->getPage($url);

        //echo $page;die;
        //echo 'mhx_page:',$page,'<br />';
        if($page !== false) {
            $content = file_get_contents($url);
            if($content != '') {
                $temp_page = 1;
                $url_temp = '';

                while ($temp_page <= $page) {
                    $urllist = array();
                    echo 'yachang',$temp_page;
                    $url_temp = $url . '-' . $temp_page;
                    $urllist = $this->getUrlList($url_temp);
                    /*print_r($urllist);
                    echo '<br />';
                    continue;*/
                    //var_dump($urllist);
                    //echo '<br />';die;
                    $temp_i = 0;
                    while (isset($urllist[$temp_i])) {

                        $this->analysePageOneExtend(false, $url_prefix, $urllist[$temp_i], $endtime);
                        //die;
                        ++$temp_i;
                    }
                    //$urllist = array();
                    ++$temp_page;
                    //die;
                }
            }
        }

        $url = 'http://exhibit.artron.net/exhibition/5-0-0';
        $page = $this->getPage($url);
        //echo 'mhx_page:',$page,'<br />';die;
        if($page !== false) {
            $content = file_get_contents($url);
            if($content != '') {
                $temp_page = 1;
                $url_temp = '';
                $urllist = array();
                while ($temp_page <= $page) {
                    echo 'yachang',$temp_page;

                    $url_temp = $url . '-' . $temp_page;
                    $urllist = $this->getUrlList($url_temp);
                    /*print_r($urllist);
                    echo '<br />';
                    continue;*/
                    /*echo '预下载列表:';
                    var_dump($urllist);
                    echo '<br />';*/
                    $temp_i = 0;
                    while (isset($urllist[$temp_i])) {

                        $this->analysePageOneExtend(false, $url_prefix, $urllist[$temp_i], $endtime);
                        ++$temp_i;
                    }
                    $urllist = array();
                    ++$temp_page;
                }
            }
        }

        $url = 'http://exhibit.artron.net/exhibition/2-0-0';
        $page = 400;
        //echo 'mhx_page:',$page,'<br />';die;
        if($page !== false) {
            $content = file_get_contents($url);
            if($content !='') {
                $temp_page = 1;
                $url_temp = '';
                $urllist = array();
                while ($temp_page <= $page) {
                    echo 'yachang',$temp_page;

                    $url_temp = $url . '-' . $temp_page;
                    $urllist = $this->getUrlList($url_temp);
                    //echo 'mhx_urllist:';
                    var_dump($urllist);
                    //echo '<br />';
                    $temp_i = 0;
                    while (isset($urllist[$temp_i])) {
                        //echo 'mhx_extend:',$url_prefix,'<br />',$urllist[$temp_i],'<br />';
                        $this->analysePageOneExtend(false, $url_prefix, $urllist[$temp_i], $endtime);
                        ++$temp_i;
                    }
                    $urllist = array();
                    ++$temp_page;
                }
            }
        }


        return null;
    }

    protected function getPage($url){
        $contents = file_get_contents($url);

//getPage
        $pos = mb_strpos($contents,'listJump mt1">');
        if($pos === false) return false;
        $pos = $pos + mb_strlen('listJump mt1">');
        $contents_temp = mb_substr($contents,$pos);

        $pos = mb_strpos($contents_temp,'<div');
        $contents_temp = mb_substr($contents_temp,0,$pos);

        $pos = mb_strpos($contents_temp,'-->');
        $contents_temp = mb_substr($contents_temp,$pos+mb_strlen('-->'));
        $page_arr = array();
        $pos = mb_strpos($contents_temp,'href="');
        while($pos !== false){
            $contents_temp = mb_substr($contents_temp,$pos+mb_strlen('href="'));
            $pos = mb_strpos($contents_temp,'"');
            $page_arr[] = mb_substr($contents_temp,0,$pos);
            $pos = mb_strpos($contents_temp,'href="');
        }

        $temp_i = 0;
        $page_int = array();
        $temp_temp = '';
        while(isset($page_arr[$temp_i])){
            if(isset(explode('-',$page_arr[$temp_i])[3])){
                $temp_temp = explode('-',$page_arr[$temp_i])[3];
            }
//            print_r($temp_temp[0]);die;
            $page_int[] = intval($temp_temp);
            ++$temp_i;
        }

        rsort($page_int);
        $page = $page_int[0];

        return $page;
    }

    protected function getUrlList($url){
        $contents = file_get_contents($url);
        $pos = mb_strpos($contents,'exList">');
        if($pos === false){return array();}
        $contents = mb_substr($contents,$pos+mb_strlen('exList">'));
        $pos = mb_strpos($contents,'上一页</a');
        $contents = mb_substr($contents,0,$pos);

        $urllist = array();
        //$temp_i = 0;
        $pos = mb_strpos($contents,'href="/exhibition');
        $contents = mb_substr($contents,$pos + mb_strlen('href="/exhibition'));
        $pos = mb_strpos($contents,'"');
        $urllist[] = '/exhibition'.mb_substr($contents,0,$pos);
        $length = mb_strlen($urllist[0]);
        while ($pos !==false){
            $contents = mb_substr($contents,$pos + mb_strlen('href="/exhibition'));
            $pos = mb_strpos($contents,'"');
            if(mb_strlen('/exhibition'.mb_substr($contents,0,$pos))<$length+10){
                $urllist[] = '/exhibition'.mb_substr($contents,0,$pos);
            }
            $pos = mb_strpos($contents,'href="/exhibition');
        }
        $urllist = array_unique($urllist);
        $urllist = array_merge($urllist);

        return $urllist;
    }


    protected function analyseFuture($endtime = 1){
        //抓取展览预告
        return null;
    }

    protected function analyseHistory($url_num, $endtime)
    {
        //是否抓取历史展览

        //如果超出所需历史时间则返回'timeout'，否则返回任意值
        return 'is_timeout';
    }

    protected function analysePageOneExtend($is_history, $url_prefix, $detailsurl, $endtime)
    {
        echo 'yachang正在运作--->';
        //echo $url_prefix,'<br />',$detailsurl,'<br />';die;
        //该方法满足网站详情页的抓取
        $common = new commonController();
        $url = $url_prefix.$detailsurl;
        /*echo $url,'-------------------------';
        return ;*/
        /*echo $url,'<br />';
        return ;*/
        //echo $url;

        echo 1;
        $findbyurl=model('maexhibitions')->find("url='".$url."'");
        if($findbyurl){
            return false;
        }


        echo 2;
        //$contents = file_get_contents($url);
        //$pos = mb_strpos($contents,'');
        $contents = file_get_contents($url);


        $pos = mb_strpos($contents,'pw fix exDetail">');
        if($pos ===false){
            return false;
        }
        /*   $strlentags = mb_strlen('pw fix exDetail">');
           if($pos === false){
               $pos = mb_strpos($contents,'exList">');
               $strlentags = mb_strlen('exList">');
               if($pos === false){
                   return '';
               }

           }*/
        $contents = mb_substr($contents,$pos+mb_strlen('pw fix exDetail">'));
        $detailsInfo = array();
        $detailsInfo['url'] = $url;
        $pos = mb_strpos($contents,'h1>');
        if($pos!==false) {
            $contents = mb_substr($contents, $pos + mb_strlen('h1>'));
        }
        $pos = mb_strpos($contents,'</h1');
        if($pos !==false) {
            $detailsInfo['name'] = mb_substr($contents, 0, $pos);
        }

        $pos = mb_strpos($contents,'</ul');
        $contents_temp = mb_substr($contents,0,$pos);

        $imgUrlList = array();
        $pos = mb_strpos($contents_temp,'src="');
        $contents_temp = mb_substr($contents_temp, $pos + mb_strlen('src="'));
        $pos = mb_strpos($contents_temp,'"');
        //第一张图拿不到不带水印的
        //$detailsInfo['pagehomeimgurl'] = getBigImg(mb_substr($contents,0,$pos));
        $pos = mb_strpos($contents_temp,'src="');
        $temp_i = 0;
        while ($pos !== false) {
            $contents_temp = mb_substr($contents_temp, $pos + mb_strlen('src="'));
            $pos = mb_strpos($contents_temp,'"');

            echo 3;

            $imgUrlList[$temp_i]['imgurl'] = $this->getBigImg(mb_substr($contents_temp,0,$pos));
            $pos = mb_strpos($contents_temp,'span>');
            $contents_temp = mb_substr($contents_temp,$pos+mb_strlen('span>'));
            $pos = mb_strpos($contents_temp,'</span');
            $imgDetails = mb_substr($contents_temp,0,$pos);
            $imgUrlList[$temp_i]['name'] = $imgDetails;
            //$imgUrlList[$temp_i] = array_merge($imgUrlList[$temp_i],$this->getImgDetails($imgDetails));
            $pos = mb_strpos($contents_temp,'src="');

            //var_dump($imgUrlList);die;
            ++$temp_i;
        }
        //$imgUrlList 这里是图片的详情
        $pos = mb_strpos($contents,'exInfo">');
        $contents = mb_substr($contents,$pos + mb_strlen('exInfo">'));
        $pos = mb_strpos($contents,'exTxt">');
        $contents_temp = mb_substr($contents,0,$pos +mb_strlen('exTxt">'));
        $contents = mb_substr($contents,$pos+mb_strlen('exTxt">'));

        //echo $contents_temp;

        $posstart = mb_strpos($contents_temp,'展览时间：');
        //echo '位置',$posstart,'位置','<br />';
        if($posstart!==false){
            $temp_yoyoyo = mb_substr($contents_temp,$posstart+mb_strlen('就这么长：'));
            $posstart = mb_strpos($temp_yoyoyo,'</dd');

            $temp_yoyoyo = strip_tags(mb_substr($temp_yoyoyo,0,$posstart));

            echo 4;
            //echo $temp_yoyoyo;die;
            $time_arr = $this->getTimeDetails($temp_yoyoyo);

            //echo '进入strtotime';
            //可能需要改
            $detailsInfo['opentimestart'] =strtotime($time_arr['opentimestart']);
            $detailsInfo['opentimeend'] =strtotime($time_arr['opentimeend']);

            //echo '结束strtotime';

        }

        $posstart = mb_strpos($contents_temp,'展览城市：');
        if($posstart!==false){
            $temp_yoyoyo = mb_substr($contents_temp,$posstart+mb_strlen('就这么长：'));
            $posstart = mb_strpos($temp_yoyoyo,'</dd');
            $temp_yoyoyo = strip_tags(mb_substr($temp_yoyoyo,0,$posstart));


            echo '-',5;
            //可能需要改
            $cityAndProvince = $this->getCityDetails($temp_yoyoyo);

            $detailsInfo['city'] = $cityAndProvince['city'];
            $detailsInfo['province'] = $cityAndProvince['province'];
        }

        $posstart = mb_strpos($contents_temp,'展览地点：');
        if($posstart!==false){
            $temp_yoyoyo = mb_substr($contents_temp,$posstart+mb_strlen('就这么长：'));
            $posstart = mb_strpos($temp_yoyoyo,'</dd');
            $temp_yoyoyo = strip_tags(mb_substr($temp_yoyoyo,0,$posstart));

            $detailsInfo['address'] = preg_replace('/\s+/','', $temp_yoyoyo);
        }

        $posstart = mb_strpos($contents_temp,'主办单位：');
        if($posstart!==false){
            $temp_yoyoyo = mb_substr($contents_temp,$posstart+mb_strlen('就这么长：'));
            $posstart = mb_strpos($temp_yoyoyo,'</dd');
            $temp_yoyoyo = strip_tags(mb_substr($temp_yoyoyo,0,$posstart));

            $detailsInfo['organizers'] = preg_replace('/\s+/','', $temp_yoyoyo);
        }

        $posstart = mb_strpos($contents_temp,'协办单位：');
        if($posstart!==false){
            $temp_yoyoyo = mb_substr($contents_temp,$posstart+mb_strlen('就这么长：'));
            $posstart = mb_strpos($temp_yoyoyo,'</dd');
            $temp_yoyoyo = strip_tags(mb_substr($temp_yoyoyo,0,$posstart));

            $detailsInfo['coorganizers'] = preg_replace('/\s+/','', $temp_yoyoyo);
        }

        $posstart = mb_strpos($contents_temp,'承办单位：');
        if($posstart!==false){
            $temp_yoyoyo = mb_substr($contents_temp,$posstart+mb_strlen('就这么长：'));
            $posstart = mb_strpos($temp_yoyoyo,'</dd');
            $temp_yoyoyo = strip_tags(mb_substr($temp_yoyoyo,0,$posstart));

            $detailsInfo['coorganizers'] = preg_replace('/\s+/','', $temp_yoyoyo);
        }

        $posstart = mb_strpos($contents_temp,'参展人员：');
        if($posstart!==false){
            $temp_yoyoyo = mb_substr($contents_temp,$posstart+mb_strlen('就这么长：'));
            $posstart = mb_strpos($temp_yoyoyo,'</dd');
            $temp_yoyoyo = strip_tags(mb_substr($temp_yoyoyo,0,$posstart));

            $detailsInfo['curator'] = preg_replace('/\s+/','', $temp_yoyoyo);
        }

        /*$posstart = mb_strpos($contents_temp,'展览备注：');
        if($posstart!==false){
            $temp_yoyoyo = mb_substr($contents_temp,$posstart+mb_strlen('就这么长：'));
            $posstart = mb_strpos($temp_yoyoyo,'</dd');
            $temp_yoyoyo = strip_tags(mb_substr($temp_yoyoyo,0,$posstart));

            $detailsInfo['mark'] = preg_replace('/\s+/','', $temp_yoyoyo);
        }*/

        $pos = mb_strpos($contents,'</div');
        $detailsInfo['introduction'] = strip_tags(mb_substr($contents,0,$pos));


        //可能需要改
        /*
         *
         *
         *
         *
         */
/*        $websitename = 'Yachang';
        $model = D('Maexhibitions');
        $model_temp = D('Mawebsite');
        $model_img = D('Maexhibits');*/
        /*echo '到这了';
        $model_img->proveAlive();die;*/
/*
        $model_temp_return = $model_temp->where("name='" .$websitename."'")->select();
        $model_temp_return = $model_temp_return[0]['id'];

        if(!$model_temp_return){
            $website_arr['name'] = $websitename;
            $website_arr['url'] = 'www.artorn.net';
            $model_temp_return = $model_temp->add($website_arr);
            $model_temp_return = $model_temp->where("name='" .$websitename."'")->select();
            $model_temp_return = $model_temp_return[0]['id'];
        }

        $detailsInfo['websiteID'] = $model_temp_return;*/

echo '-',6;
        if(!is_dir(ROOT_PATH.'exhibits')) mkdir(ROOT_PATH.'exhibits');
        if(!is_dir(self::$mFilePath)) {
            mkdir(self::$mFilePath);
            chmod(self::$mFilePath,0777);
        }
        $path_temp = self::$mFilePath.date('Ymd');
        if(!is_dir($path_temp)){
            mkdir($path_temp);
            chmod($path_temp,0777);
        }

        echo '-',7;
        $findbyname=model('maexhibitions')->find("name='".$detailsInfo['name']."'");
        if(!$findbyname) {
//            print_r($detailsInfo);die;
            $exhibitionmaker = new exhibitionsMakerController();

            echo '-',8;
            $detailsInfo['companiesID'] = $exhibitionmaker->getmuseumID($detailsInfo);


            echo '-',9;
            $location = $exhibitionmaker->getlocation($detailsInfo);
            //print_r($location);

            if($location!=false) {
                $detailsInfo['longitude'] = $location['longitude'];
                $detailsInfo['latitude'] = $location['latitude'];
            }else{
                $detailsInfo['longitude'] = '';
                $detailsInfo['latitude'] = '';

            }



            $detailsInfo['curatorID'] = $exhibitionmaker->getauthorID($detailsInfo);

            /*echo '即将进入存储';
            print_r($detailsInfo);
            die;*/

            $detailsInfo['visittime'] = time();


echo '-',10;
            $cityid=$common->getcityid($detailsInfo['city']);
            $detailsInfo['cityid'] = $cityid['id'];
            if($detailsInfo['cityid']==null || $detailsInfo['cityid']=='' || $detailsInfo['cityid']==false){
                $detailsInfo['cityid'] = 0;
            }

            unset($detailsInfo['province']);
            echo '-',11;
//           print_r($detailsInfo);die;


            if($detailsInfo['opentimestart']<=0){
                $detailsInfo['opentimestart'] = 0;
            }
            if($detailsInfo['opentimeend']<=0){
                $detailsInfo['opentimeend'] = 0;
            }
            /*if($detailsInfo['']){
                $detailsInfo[''] = ;
            }*/



            $exid = model('maexhibitions')->insert($detailsInfo);
            //die;
            $temp_imgurl_i = 0;

            /*echo '查重前::';
            print_r($imgUrlList);
            $imgUrlList = array_unique($imgUrlList);
            echo '<br />','查重后:::';
            print_r($imgUrlList);die;*/

            /*        echo '<br />$imglist:::';
                    print_r($imgUrlList);
                    echo '<br />';*/
            $exhibitsmaker = new exhibitsMakerController();

//            print_r($imgUrlList);die;
            while(isset($imgUrlList[$temp_imgurl_i])) {

                /*echo '$temp_i::',$temp_imgurl_i,'<br />';
                echo $imgUrlList[$temp_imgurl_i]['imgurl'];
                echo '<br />';*/
//                print_r($imgUrlList[$temp_imgurl_i]);
                echo '-',12;
                $imgpath = $this->saveImage($imgUrlList[$temp_imgurl_i]['imgurl']);
                /*echo 'imgpath::';
                print_r($imgpath);*/
                //echo '返回imgpath::',$imgpath,'<br />';
                if ($imgpath === false) {
                    ++$temp_imgurl_i;
                    continue;
                }

                if($imgpath != '') {
                    $imgUrlList[$temp_imgurl_i]['imgpath'] = 'exhibits/' . date('Ymd') . '/' . $imgpath;
                }else{
                    ++$temp_imgurl_i;
                    continue;
                }
                $imgUrlList[$temp_imgurl_i]['exhibitionsID'] = $exid;

                echo '-',13;
                //print_r($imgUrlList[$temp_imgurl_i]);
                $ret = $exhibitsmaker->changeyachang($imgUrlList[$temp_imgurl_i]);
                //print_r($ret);
                $ret['imgurl'] = $imgUrlList[$temp_imgurl_i]['imgurl'];
                $ret['imgpath'] = $imgUrlList[$temp_imgurl_i]['imgpath'];
                $ret['exhibitionsID'] = $imgUrlList[$temp_imgurl_i]['exhibitionsID'];
                $ret['status'] = 1;
                /*echo 'ret:::';
                print_r($ret);*/
                echo '-',14;
                model('maexhibits')->insert($ret);
                echo '-',15;
                ++$temp_imgurl_i;

            }
           // die;
        }
        return null;
    }

    protected function getBigImg($url){
        $url_explode = explode('/',$url);
        if($url_explode[5] == ''|| $url_explode[4] == '' || $url_explode[3] ==''){
            return '';
        }
        $url = $url_explode[0].'/'.$url_explode[1].'/'.$url_explode[2].'/'.$url_explode[3].'/'.$url_explode[4].'/b_'.$url_explode[5];
        return $url;
    }
    protected function getImgDetails($imgDetails){
        if($imgDetails == ''){
            return array();
        }
        $imgDetails = preg_replace('/\s+/',' ',$imgDetails);
        $toReturn = array();
        $imgDetails_explode = explode(' ',$imgDetails);
        $toReturn['name'] = $imgDetails_explode[0];
        if($imgDetails_explode[1] !=''){
            $toReturn['author'] = $imgDetails_explode[1];
        }
        if($imgDetails_explode[2] !=''){
            $toReturn['size'] = $imgDetails_explode[2];
        }
        if($imgDetails_explode[3] !=''){
            $toReturn['years'] = $imgDetails_explode[3];
        }
        if($imgDetails_explode[4] !=''){
            $toReturn['type'] = $imgDetails_explode[4];
        }

        return $toReturn;
    }

    protected function getTimeDetails($time){
        //2017-02-04 - 2017-02-28   2017,02,04,2017,02,28
        $time = preg_replace('/\s+/','',$time);
        $time_arr = explode('-',$time);
        if(isset($time_arr[5])){
            if(intval($time_arr[0]) > intval($time_arr[3])){
                $toReturn['opentimestart'] = $time_arr[0].'-'.$time_arr[1].'-'.$time_arr[2];
                $toReturn['opentimeend'] = $time_arr[0].'-'.$time_arr[4].'-'.$time_arr[5];
            }else {
                $toReturn['opentimestart'] = $time_arr[0] . '-' . $time_arr[1] . '-' . $time_arr[2];
                $toReturn['opentimeend'] = $time_arr[3] . '-' . $time_arr[4] . '-' . $time_arr[5];
            }
            return $toReturn;
        }else if(isset($time_arr[4])){
            $toReturn['opentimestart'] = $time_arr[0] . '-' . $time_arr[1] . '-' . $time_arr[2];
            $toReturn['opentimeend'] = $time_arr[0] . '-' . $time_arr[3] . '-' . $time_arr[4];
            return $toReturn;
        }else{
            return '';
        }
    }

    protected function getCityDetails($city){
        $city = preg_replace('/\s+/','',$city);
        $city_arr = explode('-',$city);
        $toReturn['province'] = $city_arr[0];
        $toReturn['city'] = $city_arr[1];
        return $toReturn;
    }



    protected function analysePageAjax($url, $exhibitionsID)
    {
        //处理ajax加载的参展作品，并将这些作品存入exhibits表，返回由所有作品id组成的字符串，ID分隔符为;（分号）
    }

    protected function saveImage($img_url){

        //print_r($img_url);die;
        /*echo 'This is save Img:::';
        echo $img_url;
        echo '<br />';*/

        $folder = ROOT_PATH.'exhibits/';

        if(!is_dir($folder)){
            mkdir($folder,0777);
            //chmod($folder,0777);
        }
        $folder = $folder.date('Ymd').'/';
        if(!is_dir($folder)){
            mkdir($folder,0777);
            //chmod($folder,0777);
        }

        //echo 'url：',$img_url,'<br />';

        $pic_format = array('.bmp','.jpg','.tiff','.gif','.pcx','.tga','.exif','.jpeg','.png',
            '.fpx','.svg','.psd','.cdr','.pcd','.dxf','.ufo','.eps','.ai','.raw','.WMF');

        //echo 'saveImage:','<br />','imgurl:',$img_url,'<br />','path:',$folder,'<br />';
        $img_url = str_replace('\\','',$img_url);

        $end = explode(".", $img_url);
        $temp_i = 0;
        while(isset($end[$temp_i])){
            ++$temp_i;
        }
        $end = "." . $end[$temp_i-1];

        if(!in_array($end,$pic_format)){
            return false;
        }
        //echo 'url没问题','<br />';
        $ch = curl_init();
        //echo 'curl没问题','<br />';
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        //要头信息
        curl_setopt($ch,CURLOPT_HEADER,1);
        //不要正文
        curl_setopt($ch, CURLOPT_NOBODY, 1);

        curl_setopt($ch,CURLOPT_URL,$img_url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $contents = curl_exec($ch);
        //echo 'contents::',$contents,'<br />';

        /*$content_type = curl_getinfo($ch,CURLINFO_CONTENT_TYPE);

        echo $content_type;die;*/
        curl_close($ch);

        //echo '$contents:::',$contents,'<br />';
        $pos = mb_stripos($contents,'200');
        if($pos === false){
            $pos = mb_strpos($contents,'OK');
            if($pos === false){
                return '';
            }
        }

        $pic_name = $this->utime() . rand(1111, 9999) . $end;

        //set_time_limit(8 * 60 * 60); //限制最大的执行时间
        $destination_folder = $folder; //文件下载保存目录
        $newfname = $destination_folder . $pic_name;//文件PATH

        //echo 'newfname:',$newfname,'<br />';

        $ch = curl_init();
        try{$fp = fopen($newfname,'wb');}catch(Exception $err){
            cpError::write($err->getMessage());
            return 0;}
        //echo 'fopen is ok!!';
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
        curl_setopt($ch,CURLOPT_URL,$img_url);
        curl_setopt($ch,CURLOPT_FILE,$fp);

        curl_exec($ch);
        curl_close($ch);
        fclose($fp);

        //生成缩略图
        Image::thumb($newfname,$destination_folder.'64_'.$pic_name,'','640','640',6);


        /*$image = new \Think\Image();
        //echo $newfname;
        try{$image->open($newfname);}catch (Exception $err){return 0;}
        echo 'Thinkphp open is right!!!';
        // 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg
        //echo $destination_folder.'64_'.$pic_name;die;
        try{$image->thumb(640, 640)->save($destination_folder.'64_'.$pic_name);}catch (Exception $err){return 0;}
        //echo 'Thinkphp thumb is right!!','<br />';
        //die;*/

        return $pic_name;
    }

    function utime()
    {
        return time();
    }

    //时间加空格
    protected function gettimestr($str)
    {
        $start = mb_substr($str, 0, 10);
        $end = mb_substr($str, 10, 5);
        $str = $start . ' ' . $end;
        return $str;
    }


}